package com.example.gnenboot.demos.service.impl;

import com.example.gnenboot.demos.domain.AnalysisNMFReq;
import com.example.gnenboot.demos.domain.AnalysisReq;
import com.example.gnenboot.demos.service.ICommandRadapterService;
import com.example.gnenboot.demos.service.IRadapterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.InputStreamReader;

@Slf4j
@Service
public class CommandRadapterServiceImpl implements ICommandRadapterService {
    @Value("${dataFilePath}")
    private String dataFilePath;
    @Value("${resultFilePath}")
    private String resultFilePath;
    @Value("${sumResultFilePath}")
    private String sumResultFilePath;
    @Value("${zipResultFilePath}")
    private String zipResultFilePath;
    @Value("${resultFileCount}")
    private String countFilePath;
    @Value("${geneFile}")
    private String geneFile;
    @Value("${sysScript}")
    private String sysScript;
    @Value("${RScript1}")
    private String RScript1;
    @Value("${RScript2}")
    private String RScript2;
    @Resource
    private IRadapterService radapterService;
    @Override
    public void analysis(AnalysisReq param){
        String result = "";
        try {

            String expFiles = param.getExpFiles();
            String scriptParams = " "+expFiles+" "+param.getGene()+" "+resultFilePath+" "+sumResultFilePath+" "+param.getMaxK()+" "+param.getReps()+" "+param.getPItem()+" "+param.getClusterNum()+" "+param.getClusterAlg()+" "+param.getDistance()+" "+param.getPFeature()+" "+param.getSeed()+" "+param.getPlot();

            // 执行系统命令，例如："ls" 或 "dir"
            Process process = Runtime.getRuntime().exec(sysScript+RScript1+scriptParams);

            // 读取命令执行结果
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                result+=line;
            }

            // 等待命令执行完成
            process.waitFor();

            // 获取退出值
            result+=";code:"+process.exitValue();
            System.out.println("result="+result);
            radapterService.saveCount(Integer.valueOf(result));
        }catch (Exception ex){
            ex.printStackTrace();
        }


    }
    @Override
    public void analysisNMF(AnalysisNMFReq param){
        String result = "";
        try {

            String expFiles = param.getExpFiles();
            String scriptParams = " "+expFiles+" "+param.getGene()+" "+resultFilePath+" "+sumResultFilePath+" "+param.getMethod()+" "+param.getNrun()+" "+param.getSeed()+" "+param.getClusterNum();
            // 执行系统命令，例如："ls" 或 "dir"
            Process process = Runtime.getRuntime().exec(sysScript+RScript2+scriptParams);

            // 读取命令执行结果
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                result+=line;
            }

            // 等待命令执行完成
            process.waitFor();

            // 获取退出值
            result+=";code:"+process.exitValue();
            System.out.println("result="+result);
            radapterService.saveCount(Integer.valueOf(result));
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
}
